package org.EmploiDuTemps;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EDTWidgetConfigure extends Activity implements OnClickListener{

	Button configOkButton;
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	public static int TEMPS_MAJ = 2 * 60 * 1000;
	private int section;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		setResult(RESULT_CANCELED);

		setContentView(R.layout.config);
		
		SharedPreferences settings = getSharedPreferences(EmploiDuTemps.PREFS_NAME, 0);
	    section = settings.getInt("section", -1);
		//int section = -1;
	    if (section != -1){
	    	EditText text = (EditText) findViewById(R.id.section);
	    	text.setText(section+"");
	    }
	    
		configOkButton = (Button)findViewById(R.id.okconfig);
		configOkButton.setOnClickListener(this);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
	}
 
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

		if (arg0.getId() == R.id.okconfig){
			//final Context context = this;
	
			//AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			//appWidgetManager.
			String textString = ((TextView) findViewById(R.id.section)).getText().toString();
			int section = -1;
			if (textString == null)
				Toast.makeText(getApplicationContext(),"Veuillez rentrer une section", Toast.LENGTH_SHORT).show();
			try{
	
				section = Integer.parseInt(textString);
				
				//prepare Alarm Service to trigger Widget
				Intent intent = new Intent(EDTWidgetProvider.MY_WIDGET_UPDATE);
				PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
				AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(System.currentTimeMillis());
				calendar.add(Calendar.SECOND, 10);
				alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),TEMPS_MAJ, pendingIntent);
				EDTWidgetProvider.saveAlarmManager(alarmManager, pendingIntent, section);
				if (section != this.section){
					EDTWidgetProvider.updateTwoWeeksFromExterior(this, section, true);
				}
				
				Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
				setResult(RESULT_OK, resultValue);
				//Toast.makeText(context, "HelloWidgetConfig.onClick(): " + String.valueOf(mAppWidgetId) , Toast.LENGTH_LONG).show();
				finish();
			}
			catch(Exception e){
				Toast.makeText(getApplicationContext(), "Il faut rentrer un nombre", Toast.LENGTH_SHORT).show();
				Log.e("test", "configuration widget : "+e.getMessage()+ " - " + e.getCause().getMessage());
			}
			
			
			//RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.hellowidget_layout);
			//appWidgetManager.updateAppWidget(mAppWidgetId, views);
			//EDTWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId);

		}

		

		
	}

		

}
